﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using FluidKit.Controls;

//Cinch
using Cinch;
using System.IO;
using System.Net;

namespace Con10uumSample
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class Con10uumWindowsChrome : UserControl
    {
        private Cjc.ChromiumBrowser.WebBrowser m_ChromiumBrowser = new Cjc.ChromiumBrowser.WebBrowser();
            
        public Con10uumWindowsChrome()
        {
            InitializeComponent();
            CreateBrowser();
        }

        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            if (m_ChromiumBrowser != null)
            {
                m_ChromiumBrowser.Arrange(new Rect(arrangeBounds));
            }
            return base.ArrangeOverride(arrangeBounds);
        }

        private void CreateBrowser()
        {
            if (LayoutRoot.Children.Count == 1)
            {
                var oldBrowser = LayoutRoot.Children[0];
                if (oldBrowser.GetType().Equals(typeof(Cjc.ChromiumBrowser.WebBrowser)))
                {
                    LayoutRoot.Children.RemoveAt(0);
                }
            }

            m_ChromiumBrowser.SetValue(Grid.RowProperty,1);
            m_ChromiumBrowser.Margin = new Thickness(10);
            m_ChromiumBrowser.EnableAsyncRendering = true;
            //newBrowser.Width = 460;
            //newBrowser.Height = 390;
            //newBrowser.LayoutTransform = new ScaleTransform(0.5, 0.5, 0.5, 0.5);
            m_ChromiumBrowser.Ready += new EventHandler(newBrowser_Ready);
            LayoutRoot.Children.Add(m_ChromiumBrowser);
        }


        private void newBrowser_Ready(object sender, EventArgs e)
        {
            String url = "http://www.google.com";
            try
            {
                //WebClient fileReader = new WebClient();
                //using (Stream data = fileReader.OpenRead(url))
                //{
                //    String webText = new StreamReader(data,Encoding.UTF7).ReadToEnd();
                //    (sender as Cjc.ChromiumBrowser.WebBrowser).LoadHtml(webText);
                //}
                (sender as Cjc.ChromiumBrowser.WebBrowser).Navigate(url);
            }
            catch (WebException ex)
            {
                Console.WriteLine("Error accessing site " + ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error accessing site " + ex.Message);
            }
        }
    }
}
